import json
from modules.utils import message
from werkzeug.exceptions import HTTPException


def handle_exception(e):
    """Return JSON instead of HTML for HTTP errors."""
    # start with the correct headers and status code from the error
    if isinstance(e, HTTPException):
        return json.dumps(message.error(e.description, e.code)), 200
    else:
        msg = "system error"
        if e.args is not None and len(e.args) > 0:
            msg = e.args[0]
        return json.dumps(message.error(msg, 500)), 200
